/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package wifipositioner;

import java.util.ArrayList;
import java.util.Collection;
import org.pi4.locutil.trace.TraceEntry;

/**
 *
 * @author hh
 */
public class ModelNN implements IModelBasedWifiPositioner {
     
    public ArrayList<PositionCalculationResult> CalculatePosition(Collection<ModelBasedTrace> offlineTraces, Collection<TraceEntry> onlineTraces)
    {	
        
        ArrayList<PositionCalculationResult> resultList = new ArrayList<>();
        
         // Find the best fitted offline entry for all the online entries
        for(TraceEntry entry : onlineTraces) 
        {    
            double minDistance = Double.MAX_VALUE;
            ModelBasedTrace bestEntry = null;
            
            // Get the offline entry with the least euclidean signal strength distance
            for(ModelBasedTrace offline : offlineTraces) 
            {
                double distance = DistanceCalculator.calcDistance(offline, entry, 15);
                if(distance < minDistance) 
                {
                    bestEntry = offline;
                    minDistance = distance;
                }
            }

            if(bestEntry != null)
            {
                
                PositionCalculationResult result = new PositionCalculationResult(bestEntry.getPosition(), entry.getGeoPosition());
                resultList.add(result);
                
//                System.out.println("Estimated position: " 
//                        + bestEntry.getPosition().toStringWithoutOrientation() 
//                        + " True position: " 
//                        + entry.getGeoPosition().toStringWithoutOrientation()+ ", Signal strength: " + minDistance + " | Distance: " + bestEntry.getPosition().distance(entry.getGeoPosition()) + " m");
            }
            else
            {
//                System.out.println("No best entry found.");
            }
        }
        
        return resultList;
    }
    
}
